package top.yangbuyi.controller.doctor;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
// import top.yangbuyi.dto.RegistrationDto;
import top.yangbuyi.constants.Constants;
import top.yangbuyi.controller.BaseController;
import top.yangbuyi.domain.Dept;
import top.yangbuyi.domain.Patient;
import top.yangbuyi.domain.Registration;
import top.yangbuyi.domain.Scheduling;
import top.yangbuyi.dto.PatientDto;
import top.yangbuyi.dto.RegistrationDto;
import top.yangbuyi.dto.RegistrationFormDto;
import top.yangbuyi.dto.RegistrationQueryDto;
import top.yangbuyi.service.PatientService;
import top.yangbuyi.service.RegistrationService;
import top.yangbuyi.service.SchedulingService;
import top.yangbuyi.service.impl.DeptService;
import top.yangbuyi.utils.IdGeneratorSnowflake;
import top.yangbuyi.utils.ShiroSecurityUtils;
import top.yangbuyi.vo.AjaxResult;
import top.yangbuyi.vo.DataGridView;

import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("doctor/registration")
public class RegistrationController extends BaseController {

	  @Reference
	  private RegistrationService registrationService;
	  @Reference
	  private SchedulingService schedulingService;
	  @Reference
	  private PatientService patientService;
	  @Autowired
	  private DeptService deptService;

	  /**
	   * 分页查询挂号信息
	   */
	  @GetMapping("queryRegistrationForPage")
	  @HystrixCommand
	  public AjaxResult queryRegistrationForPage(RegistrationDto registrationDto) {
			DataGridView gridView = this.registrationService.queryRegistrationForPage(registrationDto);
			return AjaxResult.success("查询成功", gridView.getData(), gridView.getTotal());
	  }

	  @GetMapping("selectAll")
	  @HystrixCommand
	  public AjaxResult selectAll() {
			List<Scheduling> schedulings = this.schedulingService.selectAll2();
			return AjaxResult.success("查询成功", schedulings);
	  }

	  /**
	   * 添加挂号信息
	   *
	   * @param registrationFormDto
	   * @return
	   */
	  @PostMapping("addRegistration")
	  public AjaxResult addRegistration(@RequestBody @Validated RegistrationFormDto registrationFormDto) {
			PatientDto patientDto = registrationFormDto.getPatientDto();
			RegistrationDto registrationDto = registrationFormDto.getRegistrationDto();
			Patient patient = null;
			//保存患者信息
			if (StringUtils.isBlank(patientDto.getPatientId())) {
				  patientDto.setPatientId(IdGeneratorSnowflake.generatorIdWithProfix("HZ"));
				  patientDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
				  patient = patientService.addPatient(patientDto);
			} else {
				  patient = this.patientService.getPatientById(patientDto.getPatientId());
			}
			if (null == patient) {
				  return AjaxResult.fail("当前患者ID不存在，请确认后再提交");
			}
			//修改部门挂号
			Dept dept = deptService.getOne(registrationDto.getDeptId());
			//保存挂号信息
			registrationDto.setRegId(IdGeneratorSnowflake.generatorIdWithProfix("GH"));
			registrationDto.setPatientId(patient.getPatientId());
			registrationDto.setPatientName(patient.getName());
			registrationDto.setRegNumber(dept.getRegNumber() + 1);
			registrationDto.setVisitDate(registrationDto.getVisitDate().substring(0, 10));
			registrationDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
			this.registrationService.addRegistration(registrationDto);
			//修改部门挂号信息编号
			dept.setRegNumber(dept.getRegNumber() + 1);
			this.deptService.updateDeptRegNumber(dept.getDeptId(), dept.getRegNumber());
			//返回挂号编号
			return AjaxResult.success("", registrationDto.getRegId());
	  }

	  /**
	   * 查询可用的科室
	   */
	  @GetMapping("listDeptForScheduling")
	  @HystrixCommand
	  public AjaxResult listDeptForScheduling(RegistrationQueryDto registrationDto) {
			Long deptId = registrationDto.getDeptId();
			String subsectionType = registrationDto.getSubsectionType();
			String schedulingType = registrationDto.getSchedulingType();
			String schedulingDay = registrationDto.getSchedulingDay();
			List<Long> deptIds = this.schedulingService.queryDeptIdsForScheduling(deptId, subsectionType, schedulingType, schedulingDay);
			if (deptIds == null || deptIds.size() == 0) {
				  return AjaxResult.success(Collections.EMPTY_LIST);
			} else {
				  List<Dept> depts = this.deptService.listDeptByDeptIds(deptIds);
				  return AjaxResult.success(depts);
			}
	  }

	  /**
	   * 挂号收费7
	   */
	  @PutMapping("collectFee/{regId}")
	  public AjaxResult collectFee(@PathVariable String regId) {
			Registration registration = this.registrationService.queryRegistrationByRegId(regId);
			if (null == registration) {
				  return AjaxResult.fail("当前挂号单ID对应的挂号单不存在，请核对后再查询");
			}
			if (!registration.getRegStatus().equals(Constants.REG_STATUS_0)) {
				  return AjaxResult.fail("当前挂号单状态不是未收费状态，不能收费");
			}
			registration.setRegStatus(Constants.REG_STATUS_1);
			return AjaxResult.toAjax(this.registrationService.updateRegistrationById(registration));
	  }

	  /**++++++++++++
	   *
	   * 挂号退费
	   */
	  @PutMapping("doReturn/{regId}")
	  public AjaxResult doReturn(@PathVariable String regId) {
			Registration registration = this.registrationService.queryRegistrationByRegId(regId);
			if (null == registration) {
				  return AjaxResult.fail("当前挂号单ID对应的挂号单不存在，请核对后再查询");
			}
			if (!registration.getRegStatus().equals(Constants.REG_STATUS_1)) {
				  return AjaxResult.fail("当前挂号单状态不是收费状态，不能退费");
			}
			registration.setRegStatus(Constants.REG_STATUS_4);
			return AjaxResult.toAjax(this.registrationService.updateRegistrationById(registration));
	  }


	  /**
	   * 挂号作废
	   */
	  @PutMapping("doInvalid/{regId}")
	  public AjaxResult doInvalid(@PathVariable String regId) {
			Registration registration = this.registrationService.queryRegistrationByRegId(regId);
			if (null == registration) {
				  return AjaxResult.fail("当前挂号单ID对应的挂号单不存在，请核对后再查询");
			}
			if (!registration.getRegStatus().equals(Constants.REG_STATUS_4)) {
				  return AjaxResult.fail("当前挂号单状态不是退费状态，不能作废");
			}
			registration.setRegStatus(Constants.REG_STATUS_5);
			return AjaxResult.toAjax(this.registrationService.updateRegistrationById(registration));
	  }

	  /**
	   * 根据身份证号查询患者信息
	   */
	  @GetMapping("getPatientByIdCard/{idCard}")
	  public AjaxResult getPatientByIdCard(@PathVariable String idCard) {
			Patient patient = this.patientService.getPatientByIdCard(idCard);
			if (null == patient) {
				  return AjaxResult.fail(idCard + "对应的患者不存在，请在下面新建患者信息");
			} else {
				  return AjaxResult.success(patient);
			}
	  }
}
